﻿using Digitalmes.Domain.Entities.Mes.Masters;

namespace Digitalmes.Application.Mes.Masters.Materials.Queries;

/// <summary>
/// 物料分页查询。
/// </summary>
public record GetMaterialWithPagedQuery : PagedQuery, IRequest<IResult<PagedList<MdMaterialDto>>>
{
    public string? Name { get; init; }

    public string? Code { get; init; }
}

internal sealed class GetRoleWithPagedQueryHandler(IRepository<MdMaterial> repo) : IRequestHandler<GetMaterialWithPagedQuery, IResult<PagedList<MdMaterialDto>>>
{
    public async Task<IResult<PagedList<MdMaterialDto>>> Handle(GetMaterialWithPagedQuery request, CancellationToken cancellationToken)
    {
        var roles = await repo.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(request.Name), m => m.Name.Contains(request.Name!))
            .WhereIF(!string.IsNullOrEmpty(request.Code), m => m.Code == request.Code!)
            .GetPagedListAsync(request.PageNumber, request.PageSize, cancellationToken);
        return Result.From(roles.Adapt<PagedList<MdMaterialDto>>());
    }
}
